﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity.Migrations;
using SQLStore;
using Models;
using System.Linq.Expressions;

namespace Services
{
    public class ContentsServices : IDataAccess<Contents>
    {
        CMSDBContext db;
        public ContentsServices()
        {
            db = new CMSDBContext();
        }

        public ContentsServices(string connectionString)
        {
            db = new CMSDBContext(connectionString);
        }

        public int Delete(object objValue)
        {
            Guid Id = Guid.Parse(objValue.ToString());

            List<Relevance> releList = db.Set<Relevance>().Where(t => t.FirstId == Id).ToList();
            db.Set<Relevance>().RemoveRange(releList);

            var entity = db.Set<Contents>().Where(t => t.Id == Id).FirstOrDefault();
            db.Set<Contents>().Remove(entity);

            return Save();
        }

        public bool Exists(Expression<Func<Contents, bool>> exp)
        {
            return db.Set<Contents>().Any(exp);
        }

        public bool Exists(object objValue)
        {
            string name = objValue.ToString();
            return db.Set<Contents>().Any(t => t.Name == name);
        }

        public Contents GetByID(object objValue)
        {
            Guid Id = Guid.Parse(objValue.ToString());
            return db.Set<Contents>().Where(t => t.Id == Id).FirstOrDefault();
        }

        public Contents GetBySeed(int seed)
        {
            return db.Set<Contents>().Where(t => t.Seed == seed).FirstOrDefault();

        }

        public List<Contents> GetList(Expression<Func<Contents, bool>> exp)
        {
            return db.Set<Contents>().Where(exp).ToList();

        }

        public List<Contents> GetListByTag(string tagUrl, Pagination page)
        {
            List<Contents> retList = new List<Contents>();
            Tag _tag = db.Set<Tag>().Where(t => t.TagUrl == tagUrl).FirstOrDefault();
            if (_tag != null)
            {
                List<Relevance> _releList = db.Set<Relevance>().Where(t => t.TableName == "contents_tag" && t.SecondId == _tag.TagId).ToList();
                List<Guid> ids = new List<Guid>();

                _releList.ForEach(t => ids.Add(t.FirstId));
                var query = db.Set<Contents>().Where(t => ids.Contains(t.Id)).OrderByDescending(t => t.UpdateTime).Skip((page.Index - 1) * page.Count).Take(page.Count);
                page.Total = db.Set<Contents>().Where(t => ids.Contains(t.Id)).Count();
                retList = query.ToList();
            }

            return retList;
        }
        public List<Contents> GetListByCategory(string categoryUrl, int index, int PageCount)
        {
            index = index - 1;
            List<Contents> retList = new List<Contents>();
            Category _category = db.Set<Category>().Where(t => t.CategoryUrl == categoryUrl).FirstOrDefault();
            if (_category != null)
            {
                List<Relevance> _releList = db.Set<Relevance>().Where(t => t.TableName == "contents_category" && t.SecondId == _category.CategoryId).ToList();
                List<Guid> ids = new List<Guid>();

                _releList.ForEach(t => ids.Add(t.FirstId));
                var query = db.Set<Contents>().Where(t => ids.Contains(t.Id)).OrderByDescending(t => t.UpdateTime).Skip(index * PageCount).Take(PageCount);
                retList = query.ToList();
            }

            return retList;
        }
        public List<Contents> GetListByCategory(string categoryUrl, Pagination page)
        {

            List<Contents> retList = new List<Contents>();
            Category _category = db.Set<Category>().Where(t => t.CategoryUrl == categoryUrl).FirstOrDefault();
            if (_category != null)
            {
                List<Relevance> _releList = db.Set<Relevance>().Where(t => t.TableName == "contents_category" && t.SecondId == _category.CategoryId).ToList();
                List<Guid> ids = new List<Guid>();

                _releList.ForEach(t => ids.Add(t.FirstId));
                var query = db.Set<Contents>().Where(t => ids.Contains(t.Id)).OrderByDescending(t => t.UpdateTime).Skip((page.Index - 1) * page.Count).Take(page.Count);
                page.Total = db.Set<Contents>().Where(t => ids.Contains(t.Id)).Count();
                retList = query.ToList();
            }

            return retList;
        }
        

        public List<Contents> GetListBySearch(string searchKey, Pagination page)
        {

            List<Contents> retList = new List<Contents>();
            var query = db.Set<Contents>().Where(t => t.Name.Contains(searchKey)).OrderByDescending(t => t.UpdateTime).Skip((page.Index - 1) * page.Count).Take(page.Count);
            page.Total = db.Set<Contents>().Where(t => t.Name.Contains(searchKey)).Count();
            retList = query.ToList();
            return retList;
        }

        public List<Contents> GetTopList(Pagination page)
        {

            List<Contents> retList = new List<Contents>();
            var query = db.Set<Contents>().OrderByDescending(t => t.UpdateTime).Skip((page.Index - 1) * page.Count).Take(page.Count);
            page.Total = db.Set<Contents>().Count();
            retList = query.ToList();
            return retList;
        }
        public List<Contents> GetTopList(int index, int PageCount)
        {
            index = index - 1;
            List<Contents> retList = new List<Contents>();
            var query = db.Set<Contents>().OrderByDescending(t => t.UpdateTime).Skip(index * PageCount).Take(PageCount);

            retList = query.ToList();
            return retList;
        }
        public List<Contents> GetCover(int index, int PageCount)
        {
            index = index - 1;
            List<Contents> retList = new List<Contents>();
            var query = db.Set<Contents>().Where(t => t.IsCover == true).OrderByDescending(t => t.UpdateTime).Skip(index * PageCount).Take(PageCount);
            retList = query.ToList();
            return retList;
        }
        public List<Contents> GetHot(int index, int PageCount)
        {
            index = index - 1;
            List<Contents> retList = new List<Contents>();
            var query = db.Set<Contents>().Where(t => t.IsHot == true).OrderByDescending(t => t.UpdateTime).Skip(index * PageCount).Take(PageCount);
            retList = query.ToList();
            return retList;
        }
        public int Save(Contents entity)
        {
            db.Set<Contents>().AddOrUpdate(entity);
            return Save();
        }

        int Save()
        {
            return db.SaveChanges();
        }
    }
}
